import React, { useEffect, useState } from 'react'
import { TYPE } from '../../theme'
import ImageCommon from '../../assets/common/ImageCommon'
import styled from 'styled-components'
import { getTimerInfo,getDayImg } from '../../utils/getTime'
// import { isMobile } from 'react-device-detect'

// 19号 15点 16分 0秒   
// 1621408560000
// 1620899353424
//   background-image:url(${isMobile ? ImageCommon.countdown_bg_app : ImageCommon.countdown_bg_html});

const BackgroundDiv = styled.div`
  position: fixed;
  width: 100%;
  height: 100%;
  background-image:url(${ImageCommon.countdown_bg_html});
  background-position:center;
  background-size:cover;
  background-repeat: no-repeat;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: black;
  @media (max-width: 768px) {
    justify-content: center
  }
`
const JsonswapIcon = styled.img`
  width: 320px;
  height: 70px;
  margin:20px 0 20px 0px;
  @media (max-width: 768px) {
    width: 160px;
    height: 35px;
  }
`
const Content = styled.div`
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  background-image:url(${ImageCommon.countdown_bg});
  background-size:cover;
  background-repeat: no-repeat;
  width: 758px;
  height: 791px;
  @media (max-width: 768px) {
    width: 379px;
    height: 395px;
  }
`
const CountDownItem = styled.div`
  display: flex;
  flex-direction: column;
  align-items: center;
`
const RowDiv = styled.div`
  flex-direction: row;
  display: flex;
  align-items: flex-end;
`
const RowDownDiv = styled.div`
  flex-direction: row;
  display: flex;
  margin: 20px 0px 30px 0px;
  align-items: flex-end;
  @media (max-width: 768px) {
    margin: 20px 0px 5px 0px;
  }
`
const DayImg = styled.img`
  width: 188px;
  height: 314px;
  @media (max-width: 768px) {
    width: 94px;
    height: 157px;
  }
`
const DayText = styled.div`
  color: white;
  font-size: 48px;
  font-weight: bold;
  @media (max-width: 768px) {
    font-size: 24px;
  }
`
const DayDesText = styled.div`
  color: white;
  font-size: 18px;
  font-weight: bold;
  @media (max-width: 768px) {
    font-size: 9px;
  };
`
const DayDesSpace = styled.div`
  width:  30px;
  @media (max-width: 768px) {
    width: 20px;
  };
`
const Space = styled.div`
  width: 20px;
`
const TopCounDownText = styled.div`
  color: white;
  font-size: 36px;
  font-weight: bold;
  margin: 50px;
  @media (max-width: 768px) {
    font-size: 18px;
    margin: 25px;
  };
`
const endTime = 1621408560

export default function CountDown() {
  const nowTime = new Date().getTime() / 1000
  const manyTime = parseInt(endTime - nowTime + '')
  const [timerInfo,setTimerInfo] = useState(getTimerInfo(manyTime))
  useEffect(()=>{
    let tempTime = manyTime
    let interval = setInterval(()=>{
      if (tempTime <= 0){
        clearInterval(interval)
      }
      tempTime -= 1
      setTimerInfo(getTimerInfo(tempTime))
    },1000)
    return ()=>{
      clearInterval(interval)
    }
  },[])

  return (
    <BackgroundDiv>
      <JsonswapIcon src={ImageCommon.joinswarp_icon}/>
      <Content>
        <TopCounDownText>
            一 倒计时 一
        </TopCounDownText>
        <CountDownItem>
          <RowDiv>
            <DayImg src={ImageCommon.cd_0}/>
            <Space/>
            <DayImg src={getDayImg(timerInfo.day)}/>
            <TYPE.white fontSize={18} fontWeight={'bold'} color={'white'}>
                天
            </TYPE.white>
          </RowDiv>
          <RowDownDiv>
            {/* <TYPE.white fontSize={24} fontWeight={'bold'} color={'white'}>
                {timerInfo.hour}
            </TYPE.white> */}

            <DayText>
              {timerInfo.hour}
            </DayText>
            <DayDesText>
            小时
            </DayDesText>
            <DayDesSpace/>
            <DayText>
              {timerInfo.minute}
            </DayText>
            <DayDesText>
            分
            </DayDesText>
            <DayDesSpace/>
            <DayText>
              {timerInfo.second}
            </DayText>
            <DayDesText>
            秒
            </DayDesText>
          </RowDownDiv>
        </CountDownItem>
      </Content>
    </BackgroundDiv>
  )
}
